package cn.ncist.health_backend.controller;

import cn.ncist.common.RespResult;
import cn.ncist.constant.MessageConstant;
import cn.ncist.entity.QueryPageBean;
import cn.ncist.health_interface.service.CheckGroupService;
import cn.ncist.pojo.CheckGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/checkGroup")
public class CheckGroupController {

    @DubboReference
    private CheckGroupService checkGroupService;

    /**
     * 带条件分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @PostMapping("/page")
    public RespResult page(@RequestBody QueryPageBean queryPageBean){
        if(queryPageBean.getCurrentPage() == 0 || queryPageBean.getPageSize() == 0){
            queryPageBean.setPageSize(10);
            queryPageBean.setCurrentPage(1);
        }
        log.info(queryPageBean.getQueryString());
        try {
            Page<CheckGroup> pageInfo = checkGroupService.findPage(queryPageBean);
            return new RespResult(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 添加检查组
     * @param checkGroup
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @PostMapping("/add")
    public RespResult add(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        if(checkitemIds.length <= 0 || checkGroup == null){
            throw new RuntimeException("传参错误！！");
        }
//        log.info("checkGroupController中的add方法被调用了1次");
        try {
            checkGroupService.add(checkitemIds,checkGroup);
            return new RespResult(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @GetMapping("/selectCheckItemIdsByCheckGroupId")
    public RespResult findCheckItemIdsByCheckGroupId(Integer checkGroupId){
        try {
            List<Integer> checkItemIds = checkGroupService.selectCheckItemIdsByCheckGroupId(checkGroupId);
            return new RespResult(true,"查询检查项id集合成功",checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,"查询检查项id集合失败");
        }
    }
    /**
     * 修改检查组信息
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @PostMapping("/edit")
    public RespResult edit(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        if(checkitemIds.length <= 0 || checkGroup == null){
            throw new RuntimeException("传参错误！！");
        }
        try {
            checkGroupService.edit(checkitemIds,checkGroup);
            return new RespResult(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @DeleteMapping("/delete")
    public RespResult delete(@RequestBody Integer checkGroupId){
        try {
            checkGroupService.delete(checkGroupId);
            return new RespResult(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new RespResult(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @GetMapping("/findAllCheckGroup")
    public RespResult findAll(){
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new RespResult(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
        } catch (Exception e){
            e.printStackTrace();
            return new RespResult(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
